package com.licheedev.someext

import android.os.Bundle
import android.os.Parcelable
import androidx.annotation.NonNull
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
import java.io.Serializable

//////////////////////////////// Fragment扩展工具

/**
 * 创建Fragment，并设置空白的Bundle作为参数
 */
inline fun <reified T : Fragment> newBundledFragment(): T {
    val instance = T::class.java.newInstance()
    instance.arguments = Bundle()
    return instance
}

/**
 * 显示DialogFragment
 */
fun FragmentActivity.showDialog(dialog: DialogFragment) {
    dialog.show(this.supportFragmentManager, dialog.javaClass.name)
}

/**
 * 显示DialogFragment
 */
fun Fragment.showDialog(dialog: DialogFragment) {
    dialog.show(this.childFragmentManager, dialog.javaClass.name)
}


/**
 * 创建DialogFragment，并设置空白的Bundle作为参数
 */
inline fun <reified T : DialogFragment> FragmentActivity.showDialog(): T {
    val instance = T::class.java.newInstance()
    instance.arguments = Bundle()
    instance.show(this.supportFragmentManager, instance.javaClass.name)
    return instance
}

/**
 * 创建DialogFragment，并设置空白的Bundle作为参数
 */
inline fun <reified T : DialogFragment> Fragment.showDialog(): T {
    val instance = T::class.java.newInstance()
    instance.arguments = Bundle()
    instance.show(this.childFragmentManager, instance.javaClass.name)
    return instance
}

fun DialogFragment.show(@NonNull manager: FragmentManager) {
    this.show(manager, this.javaClass.name)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putByteArg(key: String, value: Byte) {
    this.arguments?.putByte(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putShortArg(key: String, value: Short) {
    this.arguments?.putShort(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putIntArg(key: String, value: Int) {
    this.arguments?.putInt(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putBooleanArg(key: String, value: Boolean) {
    this.arguments?.putBoolean(key, value)
}


/**
 * 为fragment设置参数
 */
fun Fragment.putLongArg(key: String, value: Long) {
    this.arguments?.putLong(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putFloatArg(key: String, value: Float) {
    this.arguments?.putFloat(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putDoubleArg(key: String, value: Double) {
    this.arguments?.putDouble(key, value)
}


/**
 * 为fragment设置参数
 */
fun Fragment.putStringArg(key: String, value: String) {
    this.arguments?.putString(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putParcelableArg(key: String, value: Parcelable) {
    this.arguments?.putParcelable(key, value)
}

/**
 * 为fragment设置参数
 */
fun Fragment.putSerializableArg(key: String, value: Serializable) {
    this.arguments?.putSerializable(key, value)
}


/**
 * 获取fragment参数
 */
fun Fragment.getByteArg(key: String, defaultValue: Byte = 0): Byte? {
    return this.arguments?.getByte(key, defaultValue)
}

/**
 * 获取fragment参数
 */
fun Fragment.getShortArg(key: String, defaultValue: Short = 0): Short? {
    return this.arguments?.getShort(key, defaultValue)
}

/**
 * 获取fragment参数
 */
fun Fragment.getIntArg(key: String, defaultValue: Int = 0): Int? {
    return this.arguments?.getInt(key, defaultValue)
}

/**
 * 获取fragment参数
 */
fun Fragment.getBooleanArg(key: String, defaultValue: Boolean = false): Boolean? {
    return this.arguments?.getBoolean(key, defaultValue)
}

/**
 * 获取fragment参数
 */
fun Fragment.getLongArg(key: String, defaultValue: Long = 0L): Long? {
    return this.arguments?.getLong(key, 0L)
}

/**
 * 获取fragment参数
 */
fun Fragment.getFloatArg(key: String, defaultValue: Float = 0f): Float? {
    return this.arguments?.getFloat(key, 0f)
}

/**
 * 获取fragment参数
 */
fun Fragment.getDoubleArg(key: String, defaultValue: Double = 0.0): Double? {
    return this.arguments?.getDouble(key, 0.0)
}

/**
 * 获取fragment参数
 */
fun Fragment.getStringArg(key: String, defaultValue: String? = null): String? {
    return this.arguments?.getString(key, defaultValue)
}

/**
 * 获取fragment参数
 */
fun <T : Parcelable> Fragment.getParcelableArg(
    key: String
): T? {
    return this.arguments?.getParcelable<T>(key)
}

/**
 * 获取fragment参数
 */
fun <T : Serializable> Fragment.getSerializableArg(key: String): T? {
    return this.arguments?.getSerializable(key) as T?
}












